package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserShopCartMapper;
import com.sky.service.UserShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserShopCartServiceImpl implements UserShopCartService {

    @Autowired
    private UserShopCartMapper userShopCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        //判断是新增还是添加数量
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCart1 = userShopCartMapper.get(shoppingCart);
        if(shoppingCart1 != null){
            //TODO
            //这里默认老板没有更新数据，则不需要再查询套餐或菜品的价格等
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);

            //更新购物车里的数据
            userShopCartMapper.update(shoppingCart1);
        }else {
            //如果它是空
            if(shoppingCartDTO.getDishId() != null){
                //如果是新增的菜品
                Dish byId = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setName(byId.getName());
                shoppingCart.setAmount(byId.getPrice());
            }else {
                //如果是新增的套餐
                Setmeal byId = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setName(byId.getName());
                shoppingCart.setAmount(byId.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            userShopCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {

        return userShopCartMapper.list(BaseContext.getCurrentId());
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete() {
        userShopCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 修改购物车
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //先查询购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCart1 = userShopCartMapper.get(shoppingCart);
        //判断数量是否等于一
        if(shoppingCart1.getNumber() > 1){
            shoppingCart1.setNumber(shoppingCart1.getNumber() - 1);
            userShopCartMapper.update(shoppingCart1);
        }else {
            userShopCartMapper.deleteById(shoppingCart1.getId());
        }
    }
}
